<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.8.0">
    

    

    



    <meta charset="utf-8">
    
    
    
    
    <title>使用jQuery UI widget编写有状态的插件 | 个人博客 | 一个coder的成长记录</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="jQuery,jQuery Widget">
    <meta name="description" content="使用jQuery UI widget编写有状态的插件 jQuery最好的设计在于两个方面，一是它的API，另外一个是它的插件系统。这也是jQuery这些年异军突起的原因，虽然现在遇到了新的危机，Vue，Angular，React的出现给在现代的浏览器开放带来了新的开发体验。 这两年前端库或者框架更新太快，但是如果你是一个真正的实践者，时间长了你慢慢发现其实很多的前端开发框架都是当时编程思想的不同实">
<meta name="keywords" content="jQuery,jQuery Widget">
<meta property="og:type" content="article">
<meta property="og:title" content="使用jQuery UI widget编写有状态的插件">
<meta property="og:url" content="https://betgar.github.io/2017/05/07/building-stateful-plugins-with-jquery-ui-widget/index.html">
<meta property="og:site_name" content="个人博客">
<meta property="og:description" content="使用jQuery UI widget编写有状态的插件 jQuery最好的设计在于两个方面，一是它的API，另外一个是它的插件系统。这也是jQuery这些年异军突起的原因，虽然现在遇到了新的危机，Vue，Angular，React的出现给在现代的浏览器开放带来了新的开发体验。 这两年前端库或者框架更新太快，但是如果你是一个真正的实践者，时间长了你慢慢发现其实很多的前端开发框架都是当时编程思想的不同实">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-04-12T09:42:14.470Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="使用jQuery UI widget编写有状态的插件">
<meta name="twitter:description" content="使用jQuery UI widget编写有状态的插件 jQuery最好的设计在于两个方面，一是它的API，另外一个是它的插件系统。这也是jQuery这些年异军突起的原因，虽然现在遇到了新的危机，Vue，Angular，React的出现给在现代的浏览器开放带来了新的开发体验。 这两年前端库或者框架更新太快，但是如果你是一个真正的实践者，时间长了你慢慢发现其实很多的前端开发框架都是当时编程思想的不同实">
    
        <link rel="alternate" type="application/atom+xml" title="个人博客" href="/atom.xml">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide">
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">俊男</h5>
          <a href="mailto:betgar@163.com" title="betgar@163.com" class="mail">betgar@163.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/">
                <i class="icon icon-lg icon-home"></i>
                Home
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives">
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags">
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/categories">
                <i class="icon icon-lg icon-th-list"></i>
                Categories
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/betgar" target="_blank">
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://weibo.com/u/2296734915" target="_blank">
                <i class="icon icon-lg icon-weibo"></i>
                Weibo
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/about">
                <i class="icon icon-lg icon-link"></i>
                About
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">使用jQuery UI widget编写有状态的插件</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">使用jQuery UI widget编写有状态的插件</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-05-07T12:00:00.000Z" itemprop="datePublished" class="page-time">
  2017-05-07
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/jQuery/">jQuery</a></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#使用jQuery-UI-widget编写有状态的插件"><span class="post-toc-number">1.</span> <span class="post-toc-text">使用jQuery UI widget编写有状态的插件</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#定义插件的步骤"><span class="post-toc-number">1.1.</span> <span class="post-toc-text">定义插件的步骤</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#jQuery-UI插件编写的方式"><span class="post-toc-number">1.2.</span> <span class="post-toc-text">jQuery UI插件编写的方式</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#小结"><span class="post-toc-number">1.2.0.1.</span> <span class="post-toc-text">小结</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#参考资料"><span class="post-toc-number">1.2.0.2.</span> <span class="post-toc-text">参考资料</span></a></li></ol></li></ol></li></ol>
        </nav>
    </aside>


<article id="post-building-stateful-plugins-with-jquery-ui-widget" class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">使用jQuery UI widget编写有状态的插件</h1>
        <div class="post-meta">
            <time class="post-time" title="2017-05-07 20:00:00" datetime="2017-05-07T12:00:00.000Z" itemprop="datePublished">2017-05-07</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/jQuery/">jQuery</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style="display:none">
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h1 id="使用jQuery-UI-widget编写有状态的插件"><a href="#使用jQuery-UI-widget编写有状态的插件" class="headerlink" title="使用jQuery UI widget编写有状态的插件"></a>使用jQuery UI widget编写有状态的插件</h1><blockquote>
<p>jQuery最好的设计在于两个方面，一是它的API，另外一个是它的插件系统。这也是jQuery这些年异军突起的原因，虽然现在遇到了新的危机，Vue，Angular，React的出现给在现代的浏览器开放带来了新的开发体验。</p>
<p>这两年前端库或者框架更新太快，但是如果你是一个真正的实践者，时间长了你慢慢发现其实很多的前端开发框架都是当时编程思想的不同实现。</p>
<p>本人的JavaScript编程能力，也从最初的面向过程的面条式代码，转变到面向对象和使用一些MVC方式来组织代码。废话不多说，进入正题。</p>
</blockquote>
<p>[TOC]</p>
<h2 id="定义插件的步骤"><a href="#定义插件的步骤" class="headerlink" title="定义插件的步骤"></a>定义插件的步骤</h2><blockquote>
<p>首先插件定义时，为了防止外部影响插件，一般插件定义全部放在闭包内部。</p>
</blockquote>
<ul>
<li>定义插件的闭包</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line"><span class="comment">// define you plugin</span></span><br><span class="line">&#125;)()</span><br></pre></td></tr></table></figure>
<ul>
<li>基于jQuery来定义插件</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 以step插件为例</span></span><br><span class="line">;(<span class="function"><span class="keyword">function</span> (<span class="params">$</span>) </span>&#123;</span><br><span class="line">  $.fn.step = <span class="function"><span class="keyword">function</span> (<span class="params">element, options</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// do something</span></span><br><span class="line">  &#125;</span><br><span class="line"> $.fn.step.prototype = &#123;</span><br><span class="line">   options: &#123;</span><br><span class="line">      version: <span class="string">'0.0.1'</span>,</span><br><span class="line">      name: <span class="string">'step'</span></span><br><span class="line">    &#125;</span><br><span class="line"> &#125;;</span><br><span class="line">&#125;)(jQuery)</span><br></pre></td></tr></table></figure>
<p>  使用extend扩展jQuery对象（jQuery也是这样扩展而成的）</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">;(<span class="function"><span class="keyword">function</span> (<span class="params">$</span>) </span>&#123;</span><br><span class="line">  $.extend($.fn, &#123;</span><br><span class="line">    <span class="comment">// 插件属性</span></span><br><span class="line">    options: &#123;</span><br><span class="line">      version: <span class="string">'0.0.1'</span>,</span><br><span class="line">      name: <span class="string">'step'</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;)(jQuery)</span><br></pre></td></tr></table></figure>
<ul>
<li>插件定义的大致步骤</li>
</ul>
<blockquote>
<p>举一个bootstrap插件Alert的例子。</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 以Alert为例</span></span><br><span class="line">+<span class="function"><span class="keyword">function</span> (<span class="params">$</span>) </span>&#123;</span><br><span class="line"><span class="meta">  'use strict'</span>;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// ALERT CLASS DEFINITION</span></span><br><span class="line">  <span class="comment">// ======================</span></span><br><span class="line">  <span class="comment">// Alert构造函数</span></span><br><span class="line">  <span class="keyword">var</span> Alert   = <span class="function"><span class="keyword">function</span> (<span class="params">el</span>) </span>&#123;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 公共属性</span></span><br><span class="line">  Alert.VERSION = <span class="string">'3.3.7'</span></span><br><span class="line">  Alert.TRANSITION_DURATION = <span class="number">150</span></span><br><span class="line"></span><br><span class="line">  Alert.prototype.close = <span class="function"><span class="keyword">function</span> (<span class="params">e</span>) </span>&#123;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 备份jQuery的prototype上已经有的alert</span></span><br><span class="line">  <span class="comment">// 为了后面防冲突处理使用.</span></span><br><span class="line">  <span class="keyword">var</span> old = $.fn.alert</span><br><span class="line">  $.fn.alert             = Plugin</span><br><span class="line">  $.fn.alert.Constructor = Alert</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">  <span class="comment">// ALERT NO CONFLICT</span></span><br><span class="line">  <span class="comment">// =================</span></span><br><span class="line">  <span class="comment">// 防冲突处理</span></span><br><span class="line">  $.fn.alert.noConflict = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    $.fn.alert = old</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">  <span class="comment">// ALERT PLUGIN DEFINITION</span></span><br><span class="line">  <span class="comment">// =======================</span></span><br><span class="line">  <span class="comment">// DOM-TO-Object的桥接模式</span></span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">Plugin</span>(<span class="params">option</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.each(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="keyword">var</span> $<span class="keyword">this</span> = $(<span class="keyword">this</span>)</span><br><span class="line">      <span class="keyword">var</span> data  = $<span class="keyword">this</span>.data(<span class="string">'bs.alert'</span>)</span><br><span class="line"></span><br><span class="line">      <span class="keyword">if</span> (!data) $<span class="keyword">this</span>.data(<span class="string">'bs.alert'</span>, (data = <span class="keyword">new</span> Alert(<span class="keyword">this</span>)))</span><br><span class="line">      <span class="keyword">if</span> (<span class="keyword">typeof</span> option == <span class="string">'string'</span>) data[option].call($<span class="keyword">this</span>)</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">&#125;(jQuery);</span><br></pre></td></tr></table></figure>
<p>从上面的例子，可以看出定义插件需要如下几个步骤：</p>
<ol>
<li>定义插件的构造函数。</li>
<li>定义插件的参数（包含公共属性）插件可以通过初始化参数，或者动态的改变参数，来改变自己的状态和行为。</li>
<li>定义插件的行为方法，来具体的控制插件的状态。</li>
<li>插件的公共部分（所有的插件都会涉及，例如：防冲突处理，一般提供noConflict方法；使用DOM-to-Object的桥接模式等）。</li>
</ol>
<p>栗子：Step插件</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Step插件</span></span><br><span class="line">(<span class="function"><span class="keyword">function</span> (<span class="params">$</span>) </span>&#123;</span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">  *@param &#123;Object&#125; options 动态参数</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">Step</span> (<span class="params">options</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// do something</span></span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">// 公共属性</span></span><br><span class="line">  Step.VERSION = <span class="string">'0.0.1'</span>;</span><br><span class="line">  Step.NAME = <span class="string">'step'</span>;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">// 原型定义，所有的实例(instance)都会拥有的方法</span></span><br><span class="line">  Step.prototype = &#123;</span><br><span class="line">    <span class="keyword">constructor</span>: Step, // 修正构造函数属性的指向</span><br><span class="line">    // 插件参数</span><br><span class="line">    options : &#123;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 很多插件系统的默认初始化方法</span></span><br><span class="line">    _init: <span class="function"><span class="keyword">function</span> (<span class="params">options</span>) </span>&#123;&#125;</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;)(jQuery)</span><br></pre></td></tr></table></figure>
<h2 id="jQuery-UI插件编写的方式"><a href="#jQuery-UI插件编写的方式" class="headerlink" title="jQuery UI插件编写的方式"></a>jQuery UI插件编写的方式</h2><blockquote>
<p>上面总结的定义插件步骤其实还不完整，定义一个插件，我们就要想到，插件实例从无到有，还要从有到无。我们定义的插件，需要有一个生命周期管理，而这个生命周期的管理，其实是所有插件都要处理的，其实它是一个公共部分。既然是公共部分(DRY原则)，必须是能够重用，而不用重复这部分代码。</p>
</blockquote>
<p>如果你多看几个bootstrap的插件，你会发现几乎每一个插件都有如下的代码：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"> <span class="comment">// ALERT PLUGIN DEFINITION</span></span><br><span class="line">  <span class="comment">// =======================</span></span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">Plugin</span>(<span class="params">option</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.each(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="keyword">var</span> $<span class="keyword">this</span> = $(<span class="keyword">this</span>)</span><br><span class="line">      <span class="keyword">var</span> data  = $<span class="keyword">this</span>.data(<span class="string">'plug.name'</span>)</span><br><span class="line"></span><br><span class="line">      <span class="keyword">if</span> (!data) $<span class="keyword">this</span>.data(<span class="string">'plug.name'</span>, (data = <span class="keyword">new</span> Step(<span class="keyword">this</span>)))</span><br><span class="line">      <span class="keyword">if</span> (<span class="keyword">typeof</span> option == <span class="string">'string'</span>) data[option].call($<span class="keyword">this</span>)</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 这段代码之所以会重复出现，是因为bootstrap的设计者，想减少bootstrap对其它库的依赖。</span></span><br><span class="line"><span class="comment">// 这段代码在$.widget.bridge中有非常巧妙的实现(jquery.ui.widget.js或者widget.js)。</span></span><br></pre></td></tr></table></figure>
<ul>
<li><p>使用$.Widget对象扩展控件</p>
<blockquote>
<p>jQuery widget factory 帮我们做了很多事情，尤其一些插件公共部分的生命周期管理，都帮我们实现了，所以创建一个jQuery UI组件从widget factory开始。</p>
<p>jQueryUI的widget公共模块，只依赖jQuery，没有和jQueryUI的core模块耦合，是一个纯粹的工厂函数实现，很方便定义自己的ui组件。</p>
</blockquote>
<p>举个栗子：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$.widget(<span class="string">'repay.step'</span>, &#123;</span><br><span class="line">  _create: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;&#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
</li>
<li><p>命名空间(namespace)： ‘repay.step’，其中repay是namespace；step是控件名称。<strong>ui</strong> 命名空间默认留给jquery.ui使用了。</p>
</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 调用</span></span><br><span class="line">$(<span class="string">'table'</span>).step();</span><br></pre></td></tr></table></figure>
<ul>
<li>继续step的例子</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 这个是控件的模版 --&gt;</span> </span><br><span class="line"><span class="tag">&lt;<span class="name">table</span> <span class="attr">name</span>=<span class="string">"step"</span> <span class="attr">class</span>=<span class="string">"step"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">tbody</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">tr</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">td</span> <span class="attr">class</span>=<span class="string">"text-center"</span>&gt;</span>第一步<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">tbody</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">table</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>控件的每一步有三个状态：原始状态，活动状态，焦点状态。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @date 2017-05-08</span></span><br><span class="line"><span class="comment"> * @author lijunnan</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">(<span class="function"><span class="keyword">function</span> (<span class="params">$</span>) </span>&#123;</span><br><span class="line">	<span class="keyword">var</span> baseClass = <span class="string">'step'</span>,</span><br><span class="line">		textClass = <span class="string">'text-center'</span>,</span><br><span class="line">		activeClass = <span class="string">'active'</span>,</span><br><span class="line">		focusClass = <span class="string">'js-focus'</span>;</span><br><span class="line"></span><br><span class="line">	$.widget(<span class="string">'repay.step'</span>, &#123;</span><br><span class="line">		options: &#123;</span><br><span class="line">			VERSION: <span class="string">'0.0.1'</span>,</span><br><span class="line">			NAME: <span class="string">'step'</span>,</span><br><span class="line">			template: <span class="string">'&lt;td&gt;&lt;/td&gt;'</span></span><br><span class="line">		&#125;,</span><br><span class="line">		<span class="comment">/**</span></span><br><span class="line"><span class="comment">		 * 创建控件的时候调用.</span></span><br><span class="line"><span class="comment">		 */</span></span><br><span class="line">		_create: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">			<span class="keyword">if</span> (!<span class="keyword">this</span>._haveContainer()) &#123;</span><br><span class="line">				<span class="keyword">this</span>._addContainer();</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">this</span>.refresh();</span><br><span class="line">			<span class="keyword">this</span>.$container = <span class="keyword">this</span>.element.find(<span class="string">'tr'</span>);</span><br><span class="line">		&#125;,</span><br><span class="line">		add: <span class="function"><span class="keyword">function</span> (<span class="params">name</span>) </span>&#123;</span><br><span class="line">			$(<span class="keyword">this</span>.options.template).appendTo(<span class="keyword">this</span>.$container).text(name);</span><br><span class="line">			<span class="keyword">this</span>.refresh();</span><br><span class="line">		&#125;,</span><br><span class="line">		refresh: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">			<span class="keyword">this</span>.element.addClass(baseClass);</span><br><span class="line">			<span class="keyword">this</span>.element.find(<span class="string">'td'</span>).addClass(textClass);</span><br><span class="line">		&#125;,</span><br><span class="line">		activate: <span class="function"><span class="keyword">function</span> (<span class="params">index, onFocus</span>) </span>&#123;</span><br><span class="line">			onFocus = <span class="keyword">typeof</span> onFocus === <span class="string">'boolean'</span> ? onFocus : <span class="literal">true</span>;</span><br><span class="line">			<span class="keyword">var</span> classes = onFocus ? activeClass + <span class="string">" "</span> + focusClass : activeClass;</span><br><span class="line">			<span class="keyword">this</span>.element.find(<span class="string">'td'</span>).eq(<span class="keyword">this</span>._constrainedIndex(index))</span><br><span class="line">				.addClass(classes);</span><br><span class="line">		&#125;,</span><br><span class="line">		isFocus: <span class="function"><span class="keyword">function</span> (<span class="params">index</span>) </span>&#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="keyword">this</span>.element.find(<span class="string">'td'</span>).eq(<span class="keyword">this</span>._constrainedIndex(index))</span><br><span class="line">				.hasClass(focusClass);</span><br><span class="line">		&#125;,</span><br><span class="line">		isActive: <span class="function"><span class="keyword">function</span> (<span class="params">index</span>) </span>&#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="keyword">this</span>.element.find(<span class="string">'td'</span>).eq(<span class="keyword">this</span>._constrainedIndex(index))</span><br><span class="line">				.hasClass(activeClass);</span><br><span class="line">		&#125;,</span><br><span class="line">		_haveContainer: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">			<span class="keyword">return</span> <span class="keyword">this</span>.element.find(<span class="string">'tr'</span>).length &gt; <span class="number">0</span>;</span><br><span class="line">		&#125;,</span><br><span class="line">		_addContainer: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">			<span class="keyword">this</span>.element.append(<span class="string">'&lt;tbody&gt;&lt;tr&gt;&lt;/tr&gt;&lt;/tbody&gt;'</span>);</span><br><span class="line">		&#125;,</span><br><span class="line">		_constrainedIndex: <span class="function"><span class="keyword">function</span> (<span class="params">index</span>) </span>&#123;</span><br><span class="line">			<span class="keyword">var</span> $allSteps = <span class="keyword">this</span>.element.find(<span class="string">'td'</span>),</span><br><span class="line">				lastIndex = $allSteps.length - <span class="number">1</span>;</span><br><span class="line">			<span class="keyword">if</span> (<span class="keyword">typeof</span> index !== <span class="string">'number'</span>) &#123;</span><br><span class="line">				index = <span class="number">0</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			index = index &lt; <span class="number">0</span> ? <span class="number">0</span> : index;</span><br><span class="line">			index = index &gt; lastIndex ? lastIndex : index;</span><br><span class="line">			<span class="keyword">return</span> index;</span><br><span class="line">		&#125;,</span><br><span class="line">      	_destroy: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.element.remove();</span><br><span class="line">        &#125;,</span><br><span class="line">        next: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="keyword">var</span> activeSelector = <span class="string">'.'</span> + activeClass, </span><br><span class="line">                focusSelector = <span class="string">'.'</span> + focusClass, </span><br><span class="line">                focusIndex,</span><br><span class="line">                $allSteps = <span class="keyword">this</span>.element.find(<span class="string">'td'</span>), </span><br><span class="line">                maxIndex = $allSteps.length - <span class="number">1</span>;</span><br><span class="line">            <span class="keyword">var</span> $focusNode = $allSteps.filter(activeSelector + focusSelector);</span><br><span class="line">            <span class="keyword">if</span> ($focusNode.length === <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">this</span>.activate(<span class="number">0</span>);</span><br><span class="line">                <span class="keyword">this</span>._trigger(<span class="string">'start'</span>, <span class="literal">null</span>, &#123;</span><br><span class="line">                    name: $allSteps.first().text(),</span><br><span class="line">                    index: <span class="number">0</span></span><br><span class="line">                &#125;);</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                focusIndex = <span class="keyword">this</span>._constrainedIndex($focusNode.next().index());</span><br><span class="line">                $focusNode.removeClass(focusClass)</span><br><span class="line">                    .next(<span class="string">'td'</span>).addClass(activeClass + <span class="string">' '</span> + focusClass);</span><br><span class="line">                <span class="keyword">if</span> (focusIndex === maxIndex) &#123;</span><br><span class="line">                    <span class="keyword">this</span>._trigger(<span class="string">'done'</span>, <span class="literal">null</span>, &#123;</span><br><span class="line">                    name: $focusNode.text(),</span><br><span class="line">                    index: focusIndex</span><br><span class="line">                &#125;);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">	&#125;);</span><br><span class="line">&#125;)(jQuery);</span><br></pre></td></tr></table></figure>
<p>上面的代码控件已经完成了。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 模版1 --&gt;</span> </span><br><span class="line"><span class="tag">&lt;<span class="name">table</span> <span class="attr">name</span>=<span class="string">"step"</span> <span class="attr">class</span>=<span class="string">"step"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">tbody</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">tr</span>&gt;</span></span><br><span class="line">               <span class="tag">&lt;<span class="name">td</span> <span class="attr">class</span>=<span class="string">"text-center"</span>&gt;</span>第1步<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">               <span class="tag">&lt;<span class="name">td</span> <span class="attr">class</span>=<span class="string">"text-center"</span>&gt;</span>第2步<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">               <span class="tag">&lt;<span class="name">td</span> <span class="attr">class</span>=<span class="string">"text-center"</span>&gt;</span>第3步<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">tbody</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">table</span>&gt;</span></span><br></pre></td></tr></table></figure>
<ul>
<li>控件的初始化</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 控件的初始化调用</span></span><br><span class="line">$(<span class="string">'table'</span>).step(); <span class="comment">// 这样就创建了step控件实例</span></span><br></pre></td></tr></table></figure>
<ul>
<li>控件的方法调用</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 活动状态</span></span><br><span class="line">$(<span class="string">'table'</span>).step(<span class="string">'next'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 调用带参数的方法</span></span><br><span class="line">$(<span class="string">'table'</span>).step(<span class="string">'isActive'</span>, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 销毁控件</span></span><br><span class="line">$(<span class="string">'table'</span>).step(<span class="string">'destroy'</span>);</span><br></pre></td></tr></table></figure>
<ul>
<li><p>事件</p>
<blockquote>
<p>事件是控件的很重要的一部分，事件是控件和外界其它控件解耦的关键。</p>
</blockquote>
</li>
</ul>
<p>栗子中next方法：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 触发的start</span></span><br><span class="line"><span class="keyword">this</span>._trigger(<span class="string">'start'</span>, <span class="literal">null</span>, &#123;</span><br><span class="line">  name: $allSteps.first().text(),</span><br><span class="line">  index: <span class="number">0</span></span><br><span class="line">&#125;);</span><br><span class="line"><span class="keyword">this</span>._trigger(<span class="string">'done'</span>, <span class="literal">null</span>, &#123;</span><br><span class="line">  name: $focusNode.text(),</span><br><span class="line">  index: focusIndex</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<blockquote>
<p>this._trigger方法是从<strong>jQuery.Widget继承的</strong> ，  此方法对触发事件做了特殊处理，所以控件对外发布的事件会变成<strong>控件名+事件名</strong> 。所以start和done事件，对外发布的事件是stepstart和stepdone。</p>
</blockquote>
<ul>
<li><p>监听事件</p>
<blockquote>
<p>事件对外发布之后，我们可以根据控件发布的事件，做一些其它的业务处理。</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$(<span class="built_in">document</span>).on(<span class="string">'stepstart'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">evt, data</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">var</span> args = <span class="built_in">arguments</span>;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">'stepstart'</span>);</span><br><span class="line">&#125;).on(<span class="string">'stepdone'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">var</span> args = <span class="built_in">arguments</span>;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">'stepdone'</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h4 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h4><blockquote>
<p>step已经完成了一个完整的控件例子的展示，从create控件建立控件到destory控件，整个生命周期的管理。</p>
<p>一个控件最终要的就是几个部分：</p>
<ol>
<li>初始化渲染(create)</li>
<li>控件的事件(event)</li>
<li>控件的销毁(destroy)</li>
<li>控件的扩展性(extend)</li>
<li>可配置性(configurable)：通过option来动态改变控件的状态。</li>
</ol>
</blockquote>
<h4 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h4><p><a href="https://www.smashingmagazine.com/2011/10/essential-jquery-plugin-patterns/" target="_blank" rel="noopener">https://www.smashingmagazine.com/2011/10/essential-jquery-plugin-patterns/</a></p>
<p><a href="http://www.cnblogs.com/timy/archive/2011/04/01/2001871.html" target="_blank" rel="noopener">http://www.cnblogs.com/timy/archive/2011/04/01/2001871.html</a></p>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2019-04-12T09:42:14.470Z" itemprop="dateUpdated">2019-04-12 17:42:14</time>
</span><br>


        
        原文链接：<a href="/2017/05/07/building-stateful-plugins-with-jquery-ui-widget/" target="_blank" rel="external">https://betgar.github.io/2017/05/07/building-stateful-plugins-with-jquery-ui-widget/</a>
        
    </div>
    
    <footer>
        <a href="https://betgar.github.io">
            <img src="/img/avatar.jpg" alt="俊男">
            俊男
        </a>
    </footer>
</blockquote>

        


        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/jQuery/">jQuery</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/jQuery-Widget/">jQuery Widget</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://betgar.github.io/2017/05/07/building-stateful-plugins-with-jquery-ui-widget/&title=《使用jQuery UI widget编写有状态的插件》 — 个人博客&pic=https://betgar.github.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://betgar.github.io/2017/05/07/building-stateful-plugins-with-jquery-ui-widget/&title=《使用jQuery UI widget编写有状态的插件》 — 个人博客&source=各种技术加身的coder" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://betgar.github.io/2017/05/07/building-stateful-plugins-with-jquery-ui-widget/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《使用jQuery UI widget编写有状态的插件》 — 个人博客&url=https://betgar.github.io/2017/05/07/building-stateful-plugins-with-jquery-ui-widget/&via=https://betgar.github.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://betgar.github.io/2017/05/07/building-stateful-plugins-with-jquery-ui-widget/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2017/05/31/the-data-storage-module-of-jquery/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">jQuery的数据缓存模块</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2017/04/18/query-oracle-table-locked/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">Oracle中被锁定的表</h4>
      </a>
    </div>
  
</nav>



    











    <!-- Valine Comments -->
    <div class="comments vcomment" id="comments"></div>
    <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
    <script src="//unpkg.com/valine@latest/dist/Valine.min.js"></script>
    <!-- Valine Comments script -->
    <script>
        var GUEST_INFO = ['nick','mail','link'];
        var guest_info = 'nick,mail,link'.split(',').filter(function(item){
          return GUEST_INFO.indexOf(item) > -1
        });
        new Valine({
            el: '#comments',
            notify: 'false' == 'true',
            verify: 'false' == 'true',
            appId: "kiGWA9mdoVtQlfGYA5uDBNX7-gzGzoHsz",
            appKey: "dHCOdD0oEIjJd0cJoAHHN0y3",
            avatar: "mm",
            placeholder: "Just comment it",
            guest_info: guest_info.length == 0 ? GUEST_INFO : guest_info,
            pageSize: "10"
        })
    </script>
    <!-- Valine Comments end -->







</article>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style="display:none">
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style="display:none">
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
                <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            
            <span>博客内容遵循 <a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>俊男 &copy; 2017 - 2020</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://betgar.github.io/2017/05/07/building-stateful-plugins-with-jquery-ui-widget/&title=《使用jQuery UI widget编写有状态的插件》 — 个人博客&pic=https://betgar.github.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://betgar.github.io/2017/05/07/building-stateful-plugins-with-jquery-ui-widget/&title=《使用jQuery UI widget编写有状态的插件》 — 个人博客&source=各种技术加身的coder" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://betgar.github.io/2017/05/07/building-stateful-plugins-with-jquery-ui-widget/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《使用jQuery UI widget编写有状态的插件》 — 个人博客&url=https://betgar.github.io/2017/05/07/building-stateful-plugins-with-jquery-ui-widget/&via=https://betgar.github.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://betgar.github.io/2017/05/07/building-stateful-plugins-with-jquery-ui-widget/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: false };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>





</body>
</html>
